<?php

namespace app\api\lists;


use app\common\model\task\TaskList;
use app\api\lists\BaseShopDataLists;
use app\api\logic\TaskLogic;

class TaskLists extends BaseShopDataLists
{
    /**
     * @notes 搜索条件
     * @return array
     * @author ljj
     * @date 2022/6/9 10:20 上午
     */
    public function where()
    {
        $where = [];
        if($this->params['type']){
            if($this->params['type'] == 1){
                $where = ['user_id' => $this->userId];
            }else{
                $where = ['worker_id' => $this->userId];
            }
        }else{
            $where[] =  ['user_id|worker_id' ,'=', $this->userId];
        }
        return $where;
    }

    /**
     * @notes 账户明细列表
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author ljj
     * @date 2022/6/9 10:21 上午
     */
    public function lists(): array
    {
        $lists = TaskList::field('id,user_id,worker_id,title,share_code,create_time,status,start_time,end_time,score')
            ->append(['status_desc'])
            ->with(['tasks'=>function($query){
                //必须把外键加上,否则....
                $query->field('id,task,list_id,description,worker_id,status,start_time,complete_time');
             },'worker'=>function($query){
                $query->field('id,nickname,avatar');
             },'owner'=>function($query){
                $query->field('id,nickname,avatar');
             }])
            ->where(self::where())
            ->limit($this->limitOffset, $this->limitLength)
            ->order('id','desc')
            ->select()->toArray();
        $logic = new TaskLogic();
        $usermap = $logic->collectUserInfo($lists);     
        foreach($lists as $k=>$v){
            $lists[$k]['usermap'] = $usermap;
        }
        return $lists;
    }

    /**
     * @notes 账户明细数量
     * @return int
     * @author ljj
     * @date 2022/6/9 10:21 上午
     */
    public function count(): int
    {
        return TaskList::where(self::where())->count();
    }
}